package com.fis.vaadin.component;

import java.util.ArrayList;
import java.util.List;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

public class PanelListItem extends Panel
{
	
	private Label lblContent;
	private Embedded embedded;
	private List<String> items = new ArrayList<String>();
	private VerticalLayout mainLayout;
	private String iconItemStyle;
	
	public PanelListItem()
	{
		init();
		this.setContent(mainLayout);
	}
	
	public PanelListItem(String caption, ThemeResource image)
	{
		this();
		this.setCaption(caption);
		this.setImage(image);
	}
	
	private void init()
	{
		mainLayout = new VerticalLayout();
		embedded = new Embedded();
		
		lblContent = new Label();
		lblContent.setContentMode(Label.CONTENT_XHTML);
		
		mainLayout.addComponent(embedded);
		mainLayout.addComponent(lblContent);
		
		this.setIconItemStyle("ul");
		fill();
	}
	
	public void addItem(String item)
	{
		items.add(item);
		fill();
	}
	
	public void addContent(String itemContent)
	{
		if (itemContent == null || itemContent.trim().length() <= 0)
		{
			return;
		}
		clearAllItems();
		String[] contents = TM.get(itemContent).split("@");
		for (String item : contents)
		{
			items.add(item);
		}
		fill();
	}
	
	public void addItem(int index, String item)
	{
		items.add(index, item);
		fill();
	}
	
	public void removeItem(String item)
	{
		if (items.size() > 0)
		{
			items.remove(item);
			fill();
		}
	}
	
	public void removeItem(int index)
	{
		if (items.size() > 0)
		{
			items.remove(index);
			fill();
		}
	}
	
	public void clearAllItems()
	{
		items.clear();
		fill();
	}
	
	public void clearImage()
	{
		embedded.setIcon(null);
	}
	
	public void setImage(ThemeResource image)
	{
		embedded.setIcon(image);
	}
	
	public String getIconItemStyle()
	{
		return iconItemStyle;
	}
	
	public void setIconItemStyle(String htmltag)
	{
		this.iconItemStyle = htmltag;
	}
	
	private void fill()
	{
		String str = "";
		str += "<" + this.iconItemStyle + ">";
		for (String item : items)
		{
			str += "<li>" + item + "</li>";
		}
		str += "</" + this.iconItemStyle + ">";
		lblContent.setValue(str);
		
	}
	
}
